package com.wang.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * redis工具类
 * String 字符串
 * Hash 哈希表
 * List 列表
 * Set 集合
 * SortedSet 有序集合
 */
@Slf4j
@Configuration
public class RedisUtil {

    /**
     * 获取的value为String类型
     */
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    public String str_get_str(String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }

    public String str_get_strAndDelete(String key) {
        return stringRedisTemplate.opsForValue().getAndDelete(key);
    }

    /**
     * 设置过期时间
     */
    public String str_get_strAndExpire(String key, long timeout) {
        return stringRedisTemplate.opsForValue().getAndExpire(key, timeout, TimeUnit.MILLISECONDS);
    }

    public Object str_get_obj(String key, Class<?> clazz) {
        return Utils.jsonStringToObj(str_get_str(key), clazz);
    }

    public Object str_get_objAndDelete(String key, Class<?> clazz) {
        return Utils.jsonStringToObj(str_get_strAndDelete(key), clazz);
    }

    public Object str_get_objAndExpire(String key, Class<?> clazz, long timeout) {
        return Utils.jsonStringToObj(str_get_strAndExpire(key, timeout), clazz);
    }

    public void str_set_str(String key, String value) {
        str_set_str(key, value, -1);
    }

    public void str_set_str(String key, String value, long timeout) {
        str_set_str(key, value, timeout, TimeUnit.MILLISECONDS);
    }

    public void str_set_str(String key, String value, long timeout, TimeUnit timeUnit) {
        stringRedisTemplate.opsForValue().set(key, value, timeout, timeUnit);
    }

    /**
     * 如果存在则设置值
     */
    public Boolean str_set_str_ifPresent(String key, String value) {
        return str_set_str_ifPresent(key, value, -1);
    }

    public Boolean str_set_str_ifPresent(String key, String value, long timeout) {
        return str_set_str_ifPresent(key, value, timeout, TimeUnit.MILLISECONDS);
    }

    public Boolean str_set_str_ifPresent(String key, String value, long timeout, TimeUnit timeUnit) {
        return stringRedisTemplate.opsForValue().setIfPresent(key, value, timeout, timeUnit);
    }

    /**
     * 如果不存在则设置值
     */
    public Boolean str_set_str_ifAbsent(String key, String value) {
        return str_set_str_ifAbsent(key, value, -1);
    }

    public Boolean str_set_str_ifAbsent(String key, String value, long timeout) {
        return str_set_str_ifAbsent(key, value, timeout, TimeUnit.MILLISECONDS);
    }

    public Boolean str_set_str_ifAbsent(String key, String value, long timeout, TimeUnit timeUnit) {
        return stringRedisTemplate.opsForValue().setIfAbsent(key, value, timeout, timeUnit);
    }

    public String hash_get_str(String key, String field) {
        return (String) hash_get_obj(key, field);
    }

    public Object hash_get_obj(String key, String field) {
        return stringRedisTemplate.opsForHash().get(key, field);
    }

    public void hash_set_str(String key, String field, String value) {
        hash_set_obj(key, field, value);
    }

    public void hash_set_obj(String key, String field, Object value) {
        stringRedisTemplate.opsForHash().put(key, field, value);
    }

    public void hash_set_str_ifAbsent(String key, String field, String value) {
        hash_set_obj_ifAbsent(key, field, value);
    }

    public void hash_set_obj_ifAbsent(String key, String field, Object value) {
        stringRedisTemplate.opsForHash().putIfAbsent(key, field, value);
    }
}
